Usage
var CLI = require( '@stdlib/cli-ctor' );
CLI( [options] )
Command-line interface (CLI) constructor.
var cli = new CLI();
The constructor accepts the following options
:
- pkg: package meta data, such as a
package.json
object. - version: command-line interface version. Default:
pkg.version
. - title: process title. If set to
true
, the default title is either pkg.bin.<field>
or pkg.name
. If set to a string
, the function sets the process title to the specified string. If set to false
, the function does not set the process title. - help: help text. Default:
''
. - updates:
boolean
indicating whether to check if a more recent version of a command-line interface exists in the package registry. In order to check for updates, the function requires both pkg.name
and pkg.version
meta data. Default: true
. - argv: an
array
of command-line arguments. Default: process.argv
. - options: command-line argument parser options.
To provide package meta data, such as the package name
and version
, set the pkg
option.
var opts = {
'pkg': require( './package.json' )
};
var cli = new CLI( opts );
To specify a particular command-line interface version (overriding package meta data), set the version
option.
var opts = {
'pkg': {
'name': 'beep',
'version': '1.1.1'
},
'version': '1.1.1-beta'
};
var cli = new CLI( opts );
cli.version();
By default, an instance sets the process title to either the first key in pkg.bin
or to pkg.name
. To explicitly set the process title, set the title
option.
var proc = require( 'process' );
var opts = {
'title': 'beep-boop'
};
var cli = new CLI( opts );
console.log( proc.title );
To disable setting the process title, set the title
option to false
.
var opts = {
'title': false
};
var cli = new CLI( opts );
When the command-line flag --help
is set, a command-line interface instance prints help text and exits the calling process. To specify the printed text, set the help
option.
var opts = {
'help': 'Usage: boop [options] <beep>',
'argv': [
'/usr/local/bin/node',
'foo.js',
'--help'
]
};
var cli = new CLI( opts );
By default, an instance resolves command-line arguments and flags via process.argv
. To specify a custom set of command-line arguments, set the argv
option.
var opts = {
'argv': [
'/usr/local/bin/node',
'foo.js',
'a',
'b',
'c'
]
};
var cli = new CLI( opts );
var args = cli.args();
To specify command-line argument parser options, such as command-line flag types and aliases, set the options
option.
var opts = {
'options': {
'boolean': [
'help',
'version'
],
'string': [
'output'
],
'alias': {
'help': [
'h'
],
'version': [
'V'
],
'output': [
'o'
]
}
},
'argv': [
'/usr/local/bin/node',
'foo.js',
'-o=bar.js'
]
};
var cli = new CLI( opts );
var flags = cli.flags();
By default, if provided sufficient package meta data (package name
and version
), an instance checks whether a newer version of a command-line interface exists in the package registry. If a newer version exists, an instance writes a message to stdout
indicating that a newer version exists. To disable this check, set the updates
option to false
.
var opts = {
'updates': false
};
var cli = new CLI( opts );
Prototype Methods
CLI.prototype.close( [code] )
Gracefully exits a command-line interface and the calling process.
var cli = new CLI();
cli.close();
To specify an exit code, provide a code
argument.
var cli = new CLI();
cli.close( 1 );
CLI.prototype.error( error[, code] )
Prints an error message to stderr
and exits a command-line interface and the calling process.
var cli = new CLI();
var err = new Error( 'invalid argument' );
cli.error( err );
When exiting due to an error, the default exit code is 1
. To specify an alternative exit code, provide a code
argument.
var cli = new CLI();
var err = new Error( 'invalid argument' );
cli.error( err, 2 );
CLI.prototype.exit( [code] )
Forcefully exits a command-line interface and the calling process.
var cli = new CLI();
cli.exit();
To specify an exit code, provide a code
argument.
var cli = new CLI();
cli.exit( 1 );
Instance Methods
cli.args()
Returns a list of command-line arguments.
var cli = new CLI({
'argv': [
'/usr/local/bin/node',
'foo.js',
'a',
'--b',
'c',
'd'
]
});
var args = cli.args();
cli.flags()
Returns command-line flags.
var cli = new CLI({
'argv': [
'/usr/local/bin/node',
'foo.js',
'a',
'--b',
'c',
'-def',
'--g=h',
'i'
]
});
var flags = cli.flags();
cli.help( [code] )
Prints help text to stderr
and then exits the calling process.
var cli = new CLI({
'help': 'Usage: beep [options] <boop>'
});
cli.help();
By default, the process exits with an exit code equal to 0
. To exit with a different exit code, provide a code
argument.
cli.version()
Prints the command-line interface version to stderr
and then exits the calling process.
var cli = new CLI({
'version': '1.1.1'
});
cli.version();